import 'dart:async';

import 'package:event_bus/event_bus.dart';

/**
 * 名称：主框架页面
 */
import 'package:flutter/material.dart';
import 'package:flutter_todo_2019/common/application.dart';
import 'package:flutter_todo_2019/common/user.dart';
import 'package:flutter_todo_2019/event/tab_change_event.dart';
import 'package:flutter_todo_2019/net/dio_manager.dart';
import 'package:flutter_todo_2019/page/flags/FlagsFinishListPage.dart';
import 'package:flutter_todo_2019/page/flags/todo_add_screen.dart';
import 'package:flutter_todo_2019/page/flags/todo_list_screen_preant.dart';
import 'package:flutter_todo_2019/page/mine/MinePage.dart';
import 'package:flutter_todo_2019/page/need_dealtwith/need_dealtwith_flags.dart';
import 'package:flutter_todo_2019/page/record/RecordGroupPage.dart';
import 'package:flutter_todo_2019/page/utils/theme_util.dart';
import 'package:flutter_todo_2019/page/utils/toast_util.dart';

import 'flags/todo_complete_screen.dart';
import 'flags/todo_list_screen.dart';
import 'flags/todo_screen.dart';
import 'mine/MinePageNew.dart';

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  MyHomePageState createState() => MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  PageController _pageController = PageController();

  int selectdIndex = 0; // 当前选中项的索引
  final widgetOptions = [Text('flags'), Text('finsh'), Text('mine')];
  var pages = <Widget>[
    TodoListScreenPpreant(),
    TodoCompleteScreen("相信相信的力量"),
    MinePageNew(title: '让优秀成为一种习惯')
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    registerTabChangeEvent();
  }

  StreamSubscription subscription;

  /// 注册刷新TODO事件
  ///
  void registerTabChangeEvent() {
    subscription = Application.eventBus.on<TabChangeEvent>().listen((event) {
      setState(() {
        selectdIndex = event.mIndex;
        onItemTapped(selectdIndex);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView.builder(
        itemBuilder: (context, index) => pages[index],
        itemCount: pages.length,
        controller: _pageController,
        physics: NeverScrollableScrollPhysics(),
        onPageChanged: (index) {
          setState(() {
            selectdIndex = index;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: Icon(Icons.description), title: widgetOptions[0]),
          BottomNavigationBarItem(
              icon: Icon(Icons.check_circle), title: widgetOptions[1]),
          BottomNavigationBarItem(
              icon: Icon(Icons.account_circle), title: widgetOptions[2]),
        ],
        currentIndex: selectdIndex,
        type: BottomNavigationBarType.fixed,
        // 设置显示模式
        fixedColor: ThemeUtils.currentThemeColor,
        onTap: onItemTapped,
      ),
    );
  }

  void onItemTapped(int value) {
    _pageController.jumpToPage(value);
  }
}
